package com.falcon.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.falcon.core.utils.BeanUtils;
import com.falcon.domain.dos.MenuComponentDO;
import com.falcon.domain.po.MenuComponentPO;
import com.falcon.mvc.domain.BasePager;
import com.falcon.service.IMenuComponentService;
import com.falcon.domain.req.MenuComponentQuery;
import com.falcon.domain.req.MenuComponentRequest;
import com.falcon.mapper.IMenuComponentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.swan.mybatis.condition.Condition;
import com.swan.mybatis.condition.SelectOption;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
public class MenuComponentService implements IMenuComponentService {

    @Autowired
    private IMenuComponentMapper menuComponentMapper;

    @Override
    public Long save(MenuComponentRequest request){
        MenuComponentPO menuComponentPO = BeanUtil.copyProperties(request, MenuComponentPO.class);

        this.menuComponentMapper.insertNotNull(menuComponentPO);

        return menuComponentPO.getId();
    }

    @Override
    public Boolean update(Long id, MenuComponentRequest request){

        MenuComponentPO menuComponentPO = BeanUtil.copyProperties(request, MenuComponentPO.class);
        menuComponentPO.setId(id);

        return this.menuComponentMapper.updateNotNull(menuComponentPO);
    }

    @Override
    public Boolean deleteById(Long id) {

        return this.menuComponentMapper.deleteById(id);
    }

    @Override
    public MenuComponentDO queryById(Long id){

        MenuComponentPO menuComponentPO = this.menuComponentMapper.selectById(id);

        MenuComponentDO menuComponentDO = BeanUtil.copyProperties(menuComponentPO, MenuComponentDO.class);

        return menuComponentDO;
    }


    @Override
    public List<MenuComponentDO> queryList(MenuComponentQuery request){

        Condition condition = request.toCondition();

        List<MenuComponentPO> poList = this.menuComponentMapper.selectList(condition);

        List<MenuComponentDO> doList = BeanUtil.copyToList(poList, MenuComponentDO.class);

        return doList;
    }

    @Override
    public BasePager<MenuComponentDO> queryPager(MenuComponentQuery request){
        SelectOption selectOption = SelectOption.newInstance().desc(MenuComponentPO.Fields.id);

        PageInfo<MenuComponentPO> pageInfo = PageHelper.startPage(request.getPage(), request.getPageSize())
                .doSelectPageInfo(() -> this.menuComponentMapper.selectList(request.toCondition(), selectOption));

        return BeanUtils.toPager(pageInfo, MenuComponentDO.class);
    }

    @Override
    public int batchDelete(List<Long> ids) {

        return this.menuComponentMapper.deleteByIds(ids);
    }


}

